<template>
    <div class="page-100 history-main-layout">
        <div v-if="historyTabs.length>1" class="history-tab-part">
            <TabsComponent
                    :tabs="historyTabs"
                    :current="tabIndex"
                    :theme="1"
                    v-on:tab-switch="switchHandler"/>
        </div>
        <div v-if="historyTabs.length>1" style="height: 1rem"></div>
        <div v-if="waitList.length > 0" class="history-content-part">
            <yd-infinitescroll :callback="loadList" ref="infinitescrollDemo">
                <yd-list theme="3" slot="list">
                    <div v-for="(item,index) in waitList">
                        <RichTextComponent
                                v-on:rich-handler="richHandlerClick"
                                :taskObj="item">
                            <!--报修单-->
                            <div v-if="item.type === 'warning'"
                                 slot="slot">
                                <div class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                            <!--场地巡检-->
                            <div v-if="item.type === 'inspection'"
                                 slot="slot">
                                <div class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                            <!--采购单-->
                            <div v-if="item.type === 'commodity'"
                                 slot="slot">
                                <div class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                            <!--凭证请领-->
                            <div v-if="item.type === 'voucher'"
                                 slot="slot">
                                <div class="wait-mobule-part gray-text-color">
                                    <span v-on:click="awaitDetailHandler(item)">查看详情</span>
                                </div>
                            </div>
                        </RichTextComponent>
                        <div class="divide-part"></div>
                    </div>
                </yd-list>
            </yd-infinitescroll>
        </div>

        <div v-else style="padding-top: 2rem">
            <img src="../../assets/task_none_task_icon.png" class="nolist-image" alt="">
            <p  class="nolist-tite">暂无描述内容</p>
        </div>

    </div>
</template>

<script>
    import TabsComponent from "@/components/TabsComponent.vue";
    import RichTextComponent from "@/components/RichTextComponent.vue";
    import {warningHistory, warningCCToMe, inspectionHistory, commodityHistory, voucherHistory} from "../../pcof/post";
    import {userInfoData,historyObject,repairOrderSavedInstance,repairOrderData,isEditRepairOrder,instObject,providerObject,} from '../../globalVariables'

    export default {
        name: "waitHistory",
        data() {
            return {
                tabIndex: 0,
                tab: 0,
                typeIndex: 0,
                historyTabs: [],
                types: [
                    [{id:0,text: "我发起的"}, {id:1,text: "抄送给我的"}],
                    [{id:0,text: ""}],
                    [{id:0,text: "我发起的"}, {id:1,text: "我审批的"}, {id:2,text: "我经办的"}],
                    [{id:0,text: ""}]
                ],
                historyObj: {
                    historyType: '',
                    index: 0,
                },
                waitList: [],
                page: 1,
                pageSize: 20,
                serviceTotalPage: 1,
            }
        },
        components: {
            RichTextComponent,
            TabsComponent,
        },
        created() {
            this.historyObj = JSON.parse(localStorage.getItem(historyObject));
            this.historyType = this.historyObj.historyType;
            this.typeIndex = this.historyObj.typeIndex;
            var index = this.historyObj.tab;

            if (index === null || index === '') {
                index = 0;
            }
            this.tab = parseInt(index);
            this.historyTabs = this.types[this.typeIndex];
            this.initFirstComeHistoryFunc();
        },
        methods: {
            async switchHandler(payload) {
                this.tab = payload.index;
                this.page = 1;
                console.log(this.$refs.infinitescrollDemo);
                if (this.$refs.infinitescrollDemo !== undefined) {
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.reInit');
                }

                this.initFirstComeHistoryFunc();
            },
            richHandlerClick(payload) {
                if (payload.taskObj.type === 'warning') {
                    if (payload.taskObj.source === '2') {
                        //打开维修单详情界面
                        //清理录入维修单相关的缓存
                        localStorage.removeItem(repairOrderSavedInstance);
                        localStorage.removeItem(repairOrderData);
                        localStorage.removeItem(isEditRepairOrder);
                        localStorage.removeItem(instObject);
                        localStorage.removeItem(providerObject);
                        localStorage.setItem(isEditRepairOrder,'0');
                        var historyObj = payload.taskObj;
                        historyObj.state = '99';
                        localStorage.setItem(repairOrderData,JSON.stringify(historyObj));
                        this.$router.push({name: "repairOrder"});
                    } else {
                        //打开报修单详情界面
                        this.$router.push({name: 'repairTask', params: {taskUUid: payload.taskObj.uuid,taskState: payload.taskObj.status,isShowEvaluateDialog: false}});
                    }
                } else if (payload.taskObj.type === 'inspection'){
                    this.$router.push({name: 'inspectionSite', params: {taskUUid: payload.taskObj.uuid,taskState: '99'}})
                } else if (payload.taskObj.type === 'commodity'){
                    //进入办公用品页
                    //type 0是待办页 1是历史页来的 currentHandler 当前的操作对象 applierUuid 发起人
                    this.$router.push({name: 'officeDetailandprogress', params: {orderUuid: payload.taskObj.uuid,state:payload.taskObj.state,currentHandler:payload.taskObj.currentHandler,applierUuid:payload.taskObj.userUuid,type:'1'}});


                } else if (payload.taskObj.type === 'voucher'){
                    //进入凭证请领界面
                    this.$router.push({name: 'voucherDetailandProgress', params: {applicationUuid: payload.taskObj.uuid,state:payload.taskObj.state,type:'1'}});

                }
            },
            awaitDetailHandler(item) {
                if (item.type === 'warning') {
                    if (item.source === '2') {
                        //打开维修单详情界面
                        //清理录入维修单相关的缓存
                        localStorage.removeItem(repairOrderSavedInstance);
                        localStorage.removeItem(repairOrderData);
                        localStorage.removeItem(isEditRepairOrder);
                        localStorage.removeItem(instObject);
                        localStorage.removeItem(providerObject);
                        localStorage.setItem(isEditRepairOrder,'0');
                        var historyObj = item;
                        historyObj.state = '99';
                        localStorage.setItem(repairOrderData,JSON.stringify(historyObj));
                        this.$router.push({name: "repairOrder"});
                    } else {
                        //打开报修单详情界面
                        this.$router.push({name: 'repairTask', params: {taskUUid: item.uuid,taskState: item.status,isShowEvaluateDialog: false}});
                    }
                } else if (item.type === 'inspection'){
                    this.$router.push({name: 'inspectionSite', params: {taskUUid: item.uuid,taskState: '99'}})
                } else if (item.type === 'commodity'){
                    //进入办公用品页
                    //type 0是待办页 1是历史页来的 currentHandler 当前的操作对象 applierUuid 发起人
                    this.$router.push({name: 'officeDetailandprogress', params: {orderUuid: item.uuid,state:item.state,currentHandler:item.currentHandler,applierUuid:item.userUuid,type:'1'}});


                } else if (item.type === 'voucher'){
                    //进入凭证请领界面
                    this.$router.push({name: 'voucherDetailandProgress', params: {applicationUuid: item.uuid,state:item.state,type:'1'}});

                }
            },
            async initFirstComeHistoryFunc() {
                //设备报修 我发起的
                this.$dialog.loading.open('加载中...');
                if (this.historyType === 'warningHistory') {
                    if (this.tab === 0) {
                        var response = await warningHistory(
                            JSON.parse(localStorage.getItem(userInfoData)).mobile,
                            this.tab,
                            1,
                            20);
                        if (response.data.code === 200) {
                            this.$dialog.loading.close();
                            this.waitList = response.data.data.list;
                            this.serviceTotalPage = response.data.data.totalPage;
                            if (this.serviceTotalPage > 1) {
                                this.page++;
                            } else {
                                /* 所有数据加载完毕 */
                                this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                                this.page = 1;
                            }
                        } else {
                            this.$dialog.loading.close();
                            this.$dialog.toast({
                                mes: response.data.msg,
                                icon: 'error',
                                timeout: 1500
                            });
                        }
                    } else if (this.tab === 1) {
                        var response = await warningCCToMe(
                            JSON.parse(localStorage.getItem(userInfoData)).mobile,
                            this.page,
                            this.pageSize
                        );
                        if (response.data.code === 200) {
                            this.$dialog.loading.close();
                            this.waitList = response.data.data.list;
                            this.serviceTotalPage = response.data.data.totalPage;
                            if (this.serviceTotalPage > 1) {
                                this.page++;
                            } else {
                                /* 所有数据加载完毕 */
                                this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                                this.page = 1;
                            }
                        } else {
                            this.$dialog.loading.close();
                            this.$dialog.toast({
                                mes: response.data.msg,
                                icon: 'error',
                                timeout: 1500
                            });
                        }
                    }

                }
                //场地巡检
                else if (this.historyType === 'inspectionHistory') {
                    var response = await inspectionHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        this.$dialog.loading.close();
                        this.waitList = response.data.data.list;
                        this.serviceTotalPage = response.data.data.totalPage;
                        if (this.serviceTotalPage > 1) {
                            this.page++;
                        } else {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                        }
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
                //办公用品 我发起的 我审批的 我经办的
                else if (this.historyType === 'commodityHistory') {
                    var response = await commodityHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.tab,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        this.$dialog.loading.close();
                        this.waitList = response.data.data.list;
                        this.serviceTotalPage = response.data.data.totalPage;
                        if (this.serviceTotalPage > 1) {
                            this.page++;
                        } else {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                        }
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
                //凭证请领
                else if (this.historyType === 'voucherHistory') {
                    var response = await voucherHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        this.$dialog.loading.close();
                        this.waitList = response.data.data.list;
                        this.serviceTotalPage = response.data.data.totalPage;
                        if (this.serviceTotalPage > 1) {
                            this.page++;
                        } else {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                        }
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
            },
            async warningHistoryFunc() {
                if (this.page > 1) {
                    var response = await warningHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.tab,
                        this.page,
                        this.pageSize);
                    if (response.data.code === 200) {
                        const _list = response.data.data.list;
                        this.waitList = [...this.waitList, ..._list];
                        if (_list.length < this.pageSize || this.page === this.serviceTotalPage) {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                            return;
                        }
                        /* 单次请求数据完毕 */
                        this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.finishLoad');

                        this.page++;
                    } else {
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                } else {
                    /* 所有数据加载完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                }
            },
            async warningCCToMeFunc() {
                if (this.page > 1) {
                    var response = await warningCCToMe(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        const _list = response.data.data.list;
                        this.waitList = [...this.waitList, ..._list];
                        if (_list.length < this.pageSize || this.page === this.serviceTotalPage) {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                            return;
                        }
                        /* 单次请求数据完毕 */
                        this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.finishLoad');

                        this.page++;
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                } else {
                    /* 所有数据加载完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                }
            },
            async inspectionHistoryFunc() {
                if (this.page > 1) {
                    var response = await inspectionHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        const _list = response.data.data.list;
                        this.waitList = [...this.waitList, ..._list];
                        if (_list.length < this.pageSize || this.page === this.serviceTotalPage) {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                            return;
                        }
                        /* 单次请求数据完毕 */
                        this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.finishLoad');

                        this.page++;
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                } else {
                    /* 所有数据加载完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                }
            },
            async commodityHistoryFunc() {
                if (this.page > 1) {
                    var response = await commodityHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.tab,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        const _list = response.data.data.list;
                        this.waitList = [...this.waitList, ..._list];
                        if (_list.length < this.pageSize || this.page === this.serviceTotalPage) {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                            return;
                        }
                        /* 单次请求数据完毕 */
                        this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.finishLoad');

                        this.page++;
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                } else {
                    /* 所有数据加载完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                }
            },
            async voucherHistoryFunc() {
                if (this.page > 1) {
                    var response = await voucherHistory(
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.page,
                        this.pageSize
                    );
                    if (response.data.code === 200) {
                        const _list = response.data.data.list;
                        this.waitList = [...this.waitList, ..._list];
                        if (_list.length < this.pageSize || this.page === this.serviceTotalPage) {
                            /* 所有数据加载完毕 */
                            this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                            this.page = 1;
                            return;
                        }
                        /* 单次请求数据完毕 */
                        this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.finishLoad');

                        this.page++;
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                } else {
                    /* 所有数据加载完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                }
            },
            async loadList() {
                if (this.historyType === 'warningHistory') {
                    if (this.tab === 0) {
                        this.warningHistoryFunc();
                    } else if (this.tab === 1) {
                        this.warningCCToMeFunc();
                    }
                }
                //场地巡检
                else if (this.historyType === 'inspectionHistory') {
                    this.inspectionHistoryFunc();
                }
                //办公用品 我发起的 我审批的  我经办的
                else if (this.historyType === 'commodityHistory') {
                    this.commodityHistoryFunc();
                }
                //凭证请领
                else if (this.historyType === 'voucherHistory') {
                    this.voucherHistoryFunc();
                }
            }
        }
    }
</script>

<style lang="scss">
    .history-main-layout {

        .history-tab-part {
            z-index:200;
            position: fixed;
            width: 100%;
            height: 1rem;
            top: 0;
            left: 0;
            padding-bottom: 1rem;
        }

        .history-content-part{

            .wait-mobule-part {
                padding-top: 0.1rem;
                padding-bottom: 0.1rem;
                font-size: 1.2em;
            }

            .divide-part {
                background-color: #e6e6e6;
                width: 100%;
                padding: 0.05rem;
            }
        }


    }
</style>